class People {
    constructor(name) {
        this.name = name;
        this.keys = {};
    }
    on (key, fn) {
        if(!this.keys[key]){
            this.keys[key] = [];
        }
        //以KEY,VALUE形式存储
        this.keys[key].push(fn)
        console.log(this.keys)
    }
    off (key, fn) {
        let keyArr = this.keys[key];
        for(let i=0; i<=keyArr.length-1; i++){
            if(fn === keyArr[i]){
                keyArr.splice(i,1)
            }
        }
    }
    emit (key, param){
        let eventArr = this.keys[key]
        //遍历及执行
        eventArr.forEach(fn=>fn(param));
    }

    sayHi() {
        console.log(`Hi, I am ${this.name}`)
    }
}

const say1 = (greeting) => {
    console.log(`${greeting}, nice meeting you.`)
}

const say2 = (greeting) => {
    console.log(`${greeting}, nice meeting you, too.`)
}

const jerry = new People('Jerry')
jerry.sayHi()
// => 输出：'Hi, I am Jerry'

jerry.on('greeting', say1)
jerry.on('greeting', say2)

jerry.emit('greeting', 'Hi')
// => 输出：'Hi, nice meeting you.' 和 'Hi, nice meeting you, too'

jerry.off('greeting', say1)
jerry.emit('greeting', 'Hi')
// => 只输出：'Hi, nice meeting you, too'